﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.fjyc.Integration.HR.Interface;
using System.Configuration;
using Oracle.DataAccess.Client;
using System.Data;

namespace SelfServiceClient
{

    public class SelfServiceClient : IEmployeeOperation
    {
        private readonly static string constr = ConfigurationManager.ConnectionStrings["SelfServiceConStr"].ConnectionString;


        public DateTime GetStartDate()
        {
            return SyncDateHelper.GetStartDate("SelfSycEmployee");
        }

        public object PushEndDate(DateTimePeroid dt)
        {
            return SyncDateHelper.PushEndDate("SelfSycEmployee", dt);
        }

        public void PushEmployee(Employee employee)
        {
            //skip empty uid
            if (string.IsNullOrEmpty(employee.O_UID))
                return;
            
            OracleConnection conn = (OracleConnection)Server;

            if (employee.STATUSCODE != "1")
            {
                try
                {
                    string deleteStr = "delete from accountcopy where accountid = :accountid";
                    List<OracleParameter> cmdParms0 = new List<OracleParameter>();
                    cmdParms0.Add(new OracleParameter("accountid", OracleDbType.NVarchar2, employee.O_UID, ParameterDirection.Input));
                    OracleCommand delcmd = new OracleCommand();
                    this.PrepareCommand(delcmd, conn, null, CommandType.Text, deleteStr, cmdParms0);
                    delcmd.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                }
            }


            //查询用户信息是否已存在
            string selectStr = "select count(*) from accountcopy where accountid = :accountid";
            List<OracleParameter> cmdParms = new List<OracleParameter>();
            cmdParms.Add(new OracleParameter("accountid", OracleDbType.NVarchar2, employee.O_UID, ParameterDirection.Input));
            OracleCommand cmd = new OracleCommand();
            this.PrepareCommand(cmd, conn, null, CommandType.Text, selectStr, cmdParms);
            int selectcount = Convert.ToInt32(cmd.ExecuteScalar());
            //如果已存在，那么更新该数据
            if (selectcount > 0)
            {
                string updateStr = "update accountcopy set username = :username, company = :company, dept = :dept, position = :position, email = :email where accountid = :accountid";
                List<OracleParameter> updateParms = new List<OracleParameter>();
                updateParms.Add(new OracleParameter("username", OracleDbType.NVarchar2, 100, (employee.CNAME == null ? "" : employee.CNAME), ParameterDirection.Input));
                updateParms.Add(new OracleParameter("company", OracleDbType.NVarchar2, 100, (employee.COMPANY == null ? "" : employee.COMPANY), ParameterDirection.Input));
                updateParms.Add(new OracleParameter("dept", OracleDbType.NVarchar2, 100, (employee.DEPT == null ? "" : employee.DEPT), ParameterDirection.Input));
                updateParms.Add(new OracleParameter("position", OracleDbType.NVarchar2, 100, (employee.POSITIONNAME == null ? "" : employee.POSITIONNAME), ParameterDirection.Input));
                updateParms.Add(new OracleParameter("email", OracleDbType.NVarchar2, 100, (employee.EMAIL == null ? "" : employee.EMAIL), ParameterDirection.Input));
                updateParms.Add(new OracleParameter("accountid", OracleDbType.NVarchar2, 100, employee.O_UID, ParameterDirection.Input));
                OracleCommand updatecmd = new OracleCommand();
                this.PrepareCommand(updatecmd, conn, null, CommandType.Text, updateStr, updateParms);
                updatecmd.ExecuteNonQuery();
            }
            //如果不存在，那么插入该数据
            else
            {
                string insertStr = "insert into accountcopy (accountid, username, company, dept, position, email) values ( :accountid, :username, :company, :dept, :position, :email)";
                List<OracleParameter> insertParms = new List<OracleParameter>();
                insertParms.Add(new OracleParameter("accountid", OracleDbType.NVarchar2, 100, employee.O_UID, ParameterDirection.Input));
                insertParms.Add(new OracleParameter("username", OracleDbType.NVarchar2, 100, (employee.CNAME == null ? "" : employee.CNAME), ParameterDirection.Input));
                insertParms.Add(new OracleParameter("company", OracleDbType.NVarchar2, 100, (employee.COMPANY == null ? "" : employee.COMPANY), ParameterDirection.Input));
                insertParms.Add(new OracleParameter("dept", OracleDbType.NVarchar2, 100, (employee.DEPT == null ? "" : employee.DEPT), ParameterDirection.Input));
                insertParms.Add(new OracleParameter("position", OracleDbType.NVarchar2, 100, (employee.POSITIONNAME == null ? "" : employee.POSITIONNAME), ParameterDirection.Input));
                insertParms.Add(new OracleParameter("email", OracleDbType.NVarchar2, 100, (employee.EMAIL == null ? "" : employee.EMAIL), ParameterDirection.Input));
                OracleCommand insertcmd = new OracleCommand();
                this.PrepareCommand(insertcmd, conn, null, CommandType.Text, insertStr, insertParms);
                insertcmd.ExecuteNonQuery();
            }

        }

        private void PrepareCommand(OracleCommand command, OracleConnection connection, OracleTransaction trans, CommandType cmdType, string cmdText, IList<OracleParameter> commandParameters)
        {
            if (connection.State != ConnectionState.Open) connection.Open();
            command.Connection = connection;
            command.CommandText = cmdText;
            command.CommandType = cmdType;
            command.BindByName = true;

            if (trans != null) command.Transaction = trans;

            if (commandParameters != null)
            {
                foreach (OracleParameter parm in commandParameters)
                {
                    if (parm != null)
                    {
                        if ((parm.Direction == ParameterDirection.InputOutput || parm.Direction == ParameterDirection.Input) && parm.Value == null)
                        {
                            parm.Value = DBNull.Value;
                        }
                        var value = parm.Value;
                        if (value.GetType() == typeof(string) || parm.OracleDbType == OracleDbType.NVarchar2)
                        {
                            if (value != DBNull.Value)
                            {
                                parm.Size = ((string)value).Length > 4000 ? -1 : 4000;
                            }
                            else
                            {
                                parm.Size = 0;
                            }
                        }
                        command.Parameters.Add(parm);
                    }
                }
            }
        }

        private OracleConnection _server;

        public object Server
        {
            get
            {
                if (_server == null)
                {
                    _server = new OracleConnection(constr);
                    _server.Open();
                }
                return _server;
            }
        }

        public void Dispose()
        {
            ((OracleConnection)Server).Close();
        }
    }
}
